package com.dazhewan.project.properties;

import com.dazhewan.project.adapter.GlobalKeyListenerAdapter;
import com.dazhewan.project.component.ScreenFrame;
import com.dazhewan.utils.FunctionUtil;
import com.dazhewan.utils.ScreenUtil;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author: zheng
 * @code date: 2022/10/10
 * @code className: GlobalListen
 * @code description: 全局监听器
 */
public class GlobalListener {
    public GlobalListener() {
        //设置日志
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            System.exit(1);
        }
        setLoggerLevel(Level.OFF);
        //添加热键监听
        addGlobalKeyListener();
    }

    private void setLoggerLevel(Level level) {
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(level);
        logger.setUseParentHandlers(false);
    }

    /**
     * @method: 添加全局按键监听器
     */
    private void addGlobalKeyListener() {
        GlobalScreen.addNativeKeyListener(new GlobalKeyListenerAdapter() {
            @Override
            public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
                final int keyCode = nativeKeyEvent.getRawCode();
//                System.out.println(keyCode);
                if (keyCode == GlobalSettting.DRAW_SCREEN_CUT_KEY_CHINESS||keyCode==GlobalSettting.DRAW_SCREEN_CUT_KEY_ENGLISH||keyCode==GlobalSettting.DRAW_SCREEN_CUT_KEY_ALL) {
                    //创建CutSccreenInfo对象
                    CutScreenInfo cutScreenInfo = new CutScreenInfo();
                    cutScreenInfo.setOperateCode(keyCode);
                    cutScreenInfo.setWindowImage(ScreenUtil.cutAllScreen());
                    ScreenFrame screenFrame = new ScreenFrame(cutScreenInfo);
                }
            }
        });
    }

}
